﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using HYCreateDuct.FormDuct;
using HYCreateDuct.MethodCreateDuct;

namespace HYCreateDuct
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class cmdCreateDuct : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			UIDocument activeUIDocument = application.ActiveUIDocument;
			List<ViewType> list = YJKRevitTools.ListViewNeed(1);
			list.Add((ViewType)3);
			list.Add((ViewType)117);
			if (!YJKRevitTools.ViewCheck(activeUIDocument, list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			if ((int)document.ActiveView.ViewType == 3 || (int)document.ActiveView.ViewType == 117)
			{
				this.CreateWorkPlane(document);
			}
			if (cmdCreateDuct.hWndRevit == null)
			{
				cmdCreateDuct.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			FormCreateDuct formCreateDuct = new FormCreateDuct(application, "绘制风管");
			formCreateDuct.Show(cmdCreateDuct.hWndRevit);
			int wMsg = 7;
			cmdCreateDuct.SendMessage(cmdCreateDuct.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			try
			{
				new CreateJigLine().CreateDuctLine(application, formCreateDuct);
			}
			catch
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private Plane CreateWorkPlane(Autodesk.Revit.DB.Document doc)
		{
			Transaction transaction = new Transaction(doc, "创建工作平面");
			transaction.Start();
			Plane plane = null;
			if (doc.ActiveView.SketchPlane == null)
			{
				string b = RevitVersionFuncs.GetViewName(doc.ActiveView).Split(new char[]
				{
					':'
				}).Last<string>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				filteredElementCollector.OfCategory(BuiltInCategory.OST_Viewers);
				foreach (Element elem in filteredElementCollector)
				{
					ElementId elementId = elem.GetParameter("无").AsElementId();
					Element element = doc.GetElement(elementId);
					if (element.Name == b)
					{
						plane = (element as SketchPlane).GetPlane();
						break;
					}
				}
				doc.ActiveView.SketchPlane = doc.CreatYJKSketchPlane(plane);
				doc.Regenerate();
			}
			else
			{
				plane = doc.ActiveView.SketchPlane.GetPlane();
			}
			transaction.Commit();
			return plane;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
